<?php
class IndexAction extends CommonAction {
    public function index(){//dump(D('Site')->getUser_name(82));
		$info = array( 
            '操作系统'=>PHP_OS,
            '运行环境'=>$_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式'=>php_sapi_name(),
            '版本'=>C('SYSTEM_NAME').C('SYSTEM_VAR').' [ <a href="#" target="_blank">查看最新版本</a> ]',
            '上传附件限制'=>ini_get('upload_max_filesize'),
            '执行时间限制'=>ini_get('max_execution_time').'秒',
            '服务器时间'=>date("Y年n月j日 H:i:s"),
            '北京时间'=>gmdate("Y年n月j日 H:i:s",time()+8*3600),
            '服务器域名/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
            '剩余空间'=>round((@disk_free_space(".")/(1024*1024)),2).'M',
            'register_globals'=>get_cfg_var("register_globals")=="1" ? "ON" : "OFF",
            'magic_quotes_gpc'=>(1===get_magic_quotes_gpc())?'YES':'NO',
            'magic_quotes_runtime'=>(1===get_magic_quotes_runtime())?'YES':'NO',
        );
		
		$this->assign('info',$info);
		$this->username=session('loginUserName');
		//dump(proOpenidReturnuinfo('ogMARt-X1gcSERykWdZ6cgPmYRzk'));
		//$db=HtM('学员基本信息');  
		//dump($db->Field('身份证号码 as uid')->limit('0,100')->select());
		//站点数量
        if(session(C('user_auth_key'))){
            $siteCount = M('Site')->count();
            $userCount = M('User')->count();
            $this->assign('siteCount',$siteCount);
            $this->assign('userCount',$userCount);
            //dump($_SESSION);
            //订单数据统计
            $orderDB  = M('Order');
            $this->stime = $this->_get('stime') ? strtotime($this->_get('stime')):$orderDB -> where(array('site_id' => $this->siteid)) -> order('create_date asc') -> getField('create_date');
            $order[0]['data'] = $orderDB -> where(array('status' => 0, 'site_id' => $this->siteid)) -> group('create_date') -> Field('count(create_date) as tp') -> select();
            $order[0]['title'] = '已经取消';
            $order[1]['data'] = $orderDB -> where(array('status' => 2, 'site_id' => $this->siteid)) -> group('create_date') -> Field('count(create_date) as tp') -> select();
            $order[1]['title'] = '已付款';
            $order[2]['data'] = $orderDB -> where(array('status' => 1, 'site_id' => $this->siteid)) -> group('create_date') -> Field('count(create_date) as tp') -> select();
            $order[2]['title'] = '待付款';
            $this->assign('order', $order);
            $myid=D('User')->where('id = '.session(C('USER_AUTH_KEY')))->getfield('member_id');//直接会员
            $this->mybelong=D('Member')->where('pid = '.$myid)->count();
            $this->myseller=count(D('Member_connect')->mySeller($myid));//直属商家
            $this->myserver=count(D('Member_connect')->getServer($myid));//两级内的服务商
            $this->balance=D('Member')->where('id = '.$myid)->getfield('money');
            //$this->totalCash=D('Member_connect')->totalCash($myid);//总收益
    //        SELECT sum(changenum) FROM `think_member_account_log` where member_id = 226717 and dateline > 1493567999 and order_id >0
            $cashwhere['order_id']=array('gt',0);

            $cashwhere['member_id']=$myid;
            $cashwhere['pdstate']=array('gt',0);
            $timek = mktime(0, 0 , 0,date("m"),1,date("Y"));
            $timej = mktime(23,59,59,date("m"),date("t"),date("Y"));
            // $cashwhere['dateline'] = array(array('gt',$timek),array('lt',$timej));


            $this->totalCash=D('Member_account_log')->where($cashwhere)->Sum('changenum');

            $timek = mktime(0, 0 , 0,date("m"),date("d"),date("Y"))-86400;
            $timej = mktime(23,59,59,date("m"),date("d"),date("Y"))-86400;
            $cashwhere['dateline'] = array(array('gt',$timek),array('lt',$timej));

            $this->yesterdayCash=D('Member_account_log')->where($cashwhere)->Sum('changenum');

            if(D('Faker')->where('id = 1')->getfield('status')==1){
                $this->allTotal=D('Faker')->where('id = 1')->getfield('money');
                $this->memberLevelOne=D('Faker')->where('id = 1')->getfield('member');
            }else{
                $this->allTotal=D('Sale')->where('pay_status = 1')->sum('total');//平台总收益
                $this->memberLevelOne=M('Supplier') -> where(1) -> count();//平台总商家
            }
            $this->getMoney=D('Member_account_log')->where('member_id = '.$myid.' and pdstate = 2')->sum('changenum');
            //商家订单
            $mybelong=D('Member_member')->where('member_id ='.$myid)->getfield('smember_id',true);
            if(count($mybelong)){
                $where='member_id in ('.implode(',',$mybelong).')';
                $salelist=D('Sale')->where($where)->order('create_time desc')->limit(8)->select();
                foreach($salelist as $k => $v){
                    $salelist[$k]['membername']=D('Member')->where('id = '.$v['member_id'])->getField('nickname');
                    $salelist[$k]['affmoney']=D('Member_account_log')->where('order_id = '.$v['order_id'].' and product_id = '.$v['product_id'])->sum('changenum');
                }
                $this->assign('salelist',$salelist);
            }
            $this->totalmoney=D('Sale')->ourtotal();//平台总营业额
            //商家后台
            $this->myLevel=D('Member')->where('id = '.$myid)->getfield('level');
            $this->myid=$myid;
            if($this->myLevel==1){
                $this->cperformance();
                if(D('Faker')->where('id = 1')->getfield('status')==1){
                    $this->allTotal=D('Faker')->where('id = 1')->getfield('money');
                    $this->memberLevelOne=D('Faker')->where('id = 1')->getfield('member');
                    $this->yesterday=D('Faker')->where('id = 1')->getfield('sixth');
                }else{
                    $this->allTotal=D('Sale')->where('pay_status = 1')->sum('total');//平台总收益
                    $this->memberLevelOne=M('Supplier') -> where(1) -> count();//平台总商家

                    $ystart = mktime(0, 0 , 0,date("m"),date("d"),date("Y"))-86400;
                    $yend = mktime(23,59,59,date("m"),date("d"),date("Y"))-86400;
                    $this->yesterday=M('Sale') -> where(array('member_id'=>$this->memberInfo['id'],'pay_status' => 1,'create_time'=>array('between',array($ystart,$yend)))) -> sum('total');
                }
            }
        }else{
            $this->redirect('Public/login');
        }

		$this->display();
    }
    //会员业绩统计
    public function cperformance(){
        $myid=D('User')->where('id = '.session(C('USER_AUTH_KEY')))->getfield('member_id');//直接会员
        $account_log = M('Member_account_log');
        //获取时间区间
        $timek = mktime(0, 0 , 0,date("m"),1,date("Y"));
        $timej = mktime(23,59,59,date("m"),date("t"),date("Y"));
        //设置查询条件
        $map['member_id'] = $myid;
        $map['dateline'] = array(array('gt',$timek),array('lt',$timej));//按月
        $map['order_id'] = array('gt',0);
        $zperSql = $account_log->where($map) -> Field('distinct order_id, product_id, attr, goodsMoney') -> buildSql();
        $zper = $account_log->table($zperSql.' a') -> Sum('goodsMoney');//总业绩
        //if(isset($_GET['zper'])){echo $account_log -> getLastSql();}
        $this -> assign('zper', $zper);
        $orderinfo = $account_log->table($zperSql.' a')->count();  //本月总订单
        $this -> assign('orderinfo', $orderinfo);
        $map['member_id'] = $myid;
        $map['order_id'] = array('GT','0');
        $map['pdstate'] = 4;
        $commission = $account_log->where($map)->Sum('changenum'); //本月总佣金（营业收益）
        if(D('Member')->where('id = '.$myid)->getfield('level') == 1){//推荐收益
            $newmap1['member_id'] = $myid;
            $newmap1['types']=1;
            $newmap1['order_id'] = array('GT','0');
            $commission1 = $account_log->where($newmap1)->Sum('changenum'); //本月总佣金
            $commission=$commission+$commission1;
        }
        $map['dateline'] = array(array('gt',mktime(0, 0 , 0,date("m"),date('d'),date("Y"))-86401),array('lt',mktime(0, 0 , 0,date("m"),date('d'),date("Y"))));//昨天
        $this->ysstercom=$account_log->where($map)->Sum('changenum');
        $this -> assign('commission', $commission);
    }
	public function upload(){
		$this -> display();
	}
	public function info(){
		if($this->_post('update')){
			if($this->_post('password') == ''){
				$this->success('没有修改密码！');exit;
			}
			$userdb = D('User');
			if(!empty($_POST['password'])){
				if($this->_post('re_password') != $this->_post('password')){
				    $this->error('您输入两次密码不一致',U('Index/info'));
					exit(); 
				}
				$data['password'] = md5(trim($_POST['password']));
			}
				$re = $userdb -> where(array('id' => session(C('USER_AUTH_KEY')))) -> save($data);
				if($re !== false){
					//A('Admin/Public') ->logout();
					$this->success('修改成功，建议请重新登录！', U('Index/index'));
				}else{
					$this->error($userdb -> getError(),U('Index/info'));
					exit();
				}
		
		}else{
			$info = M('User') -> where(array('id' =>session(C('USER_AUTH_KEY')))) -> find();
			$this->assign('info',$info);
			$this->display();
		}
	}
	
	//进入编辑模式
	public function edit(){
		session('isEdit',$this->_get('isEdit'));
		if(session('isEdit') > 0){
			//session('siteid', $_GET['siteid']);
		    $this->success('成功进入可视化编辑');
		}else{
			$this->success('成功退出可视化编辑');
		}
	}
}
?>